<?php
/**
 * 无需登录的接口
 * 获取热门分类、获取问题列表、获取内容详情、获取问题回答列表
 * @author Edwin
 */

namespace V1\Controller;

use Common\Common\Tools;
use Common\Controller\CommonController;
use Think\Exception;
use V1\Controller\ToolsController;

class NoLoginController extends CommonController
{
    /**
     * 获取热门分类
     */
    public function getHotCategory()
    {
        $request = D('Category')->get_category_list(null, null, null, null, 2);
        if (!empty($request)) {
            $this->myApiPrint(C('STATUS')['200'], 200, $request);
        } else {
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        }
    }

    /**
     * 获取问题列表
     */
    public function getQuestionList()
    {
        $info['start'] = trim(I('get.start'));
        $info['limit'] = !empty(I('get.limit')) ? trim(I('get.limit')) : '';
        $info['cat_id'] = !empty(I('get.cat_id')) ? trim(I('get.cat_id')) : '';
        $info['keyword'] = !empty(I('get.keyword')) ? trim(I('get.keyword')) : '';
        $info['userid'] = !empty(I('get.uid')) ? trim(I('get.uid')) : '';
        $data = D('QuestionAsk')->get_ask_list($info);
        if (!$data) {
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['addtime_date'] = format_date($v['addtime']);
                $data[$k]['ask_images'] = [];
                $data[$k]['image_count'] = '';
                for ($i = 0; $i < 6; $i++) {
                    if ($v['thumb' . $i]) {
                        $data[$k]['ask_images'][] = $v['thumb' . $i];
                        $data[$k]['image_count'] = $i + 1;
                    }
                    unset($data[$k]['thumb' . $i]);
                }
                $data[$k]['area'] = Tools::arr2str(D('Area')->get_area_full_name_from_area_id($v['areaid']), ' ');
            }
            $this->myApiPrint('查询数据成功', 200, $data);
        }
    }

    /**
     * 获取内容详情
     */
    public function getProblemDetails()
    {
        $askid = I('get.askid');
        if ($askid) {
            $userid = I('get.uid');
            $data = D('Ask')->get_problem_details($askid, $userid);
            foreach ($data as $k => $v) {
                $data[$k]['addtime_date'] = format_date($v['addtime']);
                for ($i = 0; $i < 6; $i++) {
                    if ($v['thumb' . $i]) {
                        $data[$k]['ask_images'][] = $v['thumb' . $i];
                        $data[$k]['image_count'] = $i + 1;
                    }
                    unset($data[$k]['thumb' . $i]);
                }
                $data[$k]['area'] = Tools::arr2str(D('Area')->get_area_full_name_from_area_id($v['areaid']), '');
            }
            if ($data) {
                $this->myApiPrint(C('STATUS')['200'], 200, $data);
            } else {
                $this->myApiPrint(C('STATUS')['230'], 201, []);
            }
        } else {
            $this->myApiPrint(C('STATUS')['101'], 400, []);
        }
    }

    /**
     * 获取问题回答列表
     */
    public function getAskArcAnswers()
    {
        //问答列表
        $info['askid'] = !empty(I('get.askid')) ? trim(I('get.askid')) : I('get.askid');
        $info['start'] = I('get.start');
        $info['limit'] = I('get.limit');
        $info['userid'] = !empty(I('get.uid')) ? trim(I('get.uid')) : null;
        $data = D('QuestionAnswer')->get_ask_arc_answers($info['askid'], $info['start'] = null, $info['limit'] = null, $info['userid']);
        foreach ($data as $k => $v) {
            $data[$k]['addtime_date'] = format_date($v['addtime']);
            $data[$k]['area'] = Tools::arr2str(D('Area')->get_area_full_name_from_area_id($v['areaid']), '');
            $data[$k]['content'] = D('QuestionAnswer')->eachKeyWord($v['content']); //获取关键词加链接
        }
        foreach ($data as $k => $v) {
            $datas[] = $v;
        }
        if ($datas) {
            $this->myApiPrint(C('STATUS')['200'], 200, $datas);
        } else {
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        }
    }

    /**
     * 忘记密码获取验证码接口
     */
    public function getVerificationCode()
    {
        $mobile = I('post.mobile');
        try {
            $result = D('Member')->getUserFromMobile($mobile);
            if ($result) {
                $k = md5($mobile) . 'reset_password_code';
                if (S($k)) {
                    $this->myApiPrint(C('STATUS')['209'], 201, false);
                } else {
                    $x = rand(100000, 999999);
                    S($k, $x, 120);
                    if ($y = inSendSms($mobile, $x)) {
                        $this->myApiPrint(C('STATUS')['200'], 200, true);
                    } else {
                        $this->myApiPrint(C('STATUS')['206'], 202, false);
                    }
                }
            } else {
                $this->myApiPrint(C('STATUS')['211'], 203, false);
            }
        } catch (Exception $exception) {
            $this->myApiPrint(C('STATUS')['201'], 400, false);
        }
    }

    /**
     * 验证验证码
     */
    public function validateCode()
    {
        $code = trim(I('post.code'));
        $mobile = trim(I('post.mobile'));
        $type = !empty(I('post.type')) ? trim(I('post.type')) : '';
        if (empty($code) || empty($mobile)) {
            $this->myApiPrint(C('STATUS')['201'], 400, false);
        } else {
            //如果类型是'register'则key等于对应的名称
            if ('register' == $type) {
                $k = md5($mobile) . 'register_code';
            } else {
                $k = md5($mobile) . 'reset_password_code';
            }
            if (S($k) == $code) {
                $this->myApiPrint(C('STATUS')['200'], 200, true);
            } else {
                $this->myApiPrint(C('STATUS')['207'], 201, false);
            }
        }
    }

    /**
     * 获取注册验证码
     */
    public function getRegisterCode()
    {
        $tools = new ToolsController();
        $mobile = trim(I('post.mobile'));
        $token = trim(I('post.token'));
        try {
            $result = D('Member')->getUserFromMobile($mobile);
            if ($result) {
                $this->myApiPrint(C('STATUS')['202'], 201, false);
            } else {
                $key = md5($mobile) . 'register_code';
                if (S($key)) {
                    $this->myApiPrint(C('STATUS')['209'], 202, false);
                } else {
                    $random_number = rand(100000, 999999);
                    S($key, $random_number, 120);
                    if ($y = $tools->sendSms($random_number, $mobile, $token)) {
                        $this->myApiPrint(C('STATUS')['200'], 200, true);
                    } else {
                        $this->myApiPrint(C('STATUS')['206'], 203, false);
                    }
                }
            }
        } catch (Exception $exception) {
            $this->myApiPrint(C('STATUS')['101'], 400, false);
        }
    }

    /**
     * 验证邀请码是否存在
     */
    public function ValidateInvitation()
    {
        $code = trim(I('post.code'));
        if (!empty($code)) {
            $result = D('MemberProfile')->check_apply_code($code);
            if ($result == 1) {
                $this->myApiPrint(C('STATUS')['200'], 200, true);
            } else {
                $this->myApiPrint(C('STATUS')['230'], 201, false);
            }
        } else {
            $this->myApiPrint(C('STATUS')['201'], 400, false);
        }

    }

    /**
     * 获取首页banner列表
     */
    public function getBanner()
    {
        $result = D('Ad')->getAd();
        if (!$result) {
            $this->myApiPrint(C('STATUS')['230'], 201, []);
        } else {
            $this->myApiPrint(C('STATUS')['200'], 200, $result);
        }
    }
}
